/*
 * @Description: 
 * @Date: 2021-08-26 11:05:24
 * @Author: 张自文
 * @LastEditTime: 2021-08-26 15:06:35
 * @LastEditors: 张自文
 * @FilePath: \flutter_demo_list\lib\pages\image_cutting\image_cutting.dart
 */
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/pages/image_cutting/image_compress_util.dart';
import 'package:get/get.dart' hide MultipartFile, FormData;
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:http_parser/http_parser.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

class ImageCuttingPage extends StatelessWidget {
  ImageCuttingPage({Key? key, this.title = ''}) : super(key: key);
  final ImageCuttingPageController vm = Get.put(ImageCuttingPageController());

  final String title;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ImageCropper',
      home: MyHomePage(
        title: 'ImageCropper',
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({required this.title});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              child: Container(
                width: 355.w,
                height: 202.w,
                decoration: BoxDecoration(
                    color: const Color.fromRGBO(247, 249, 252, 1),
                    border: Border.all(
                        width: 2.w, color: Color.fromRGBO(229, 229, 229, 1))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'assets/images/home/tianjiazhaopian.png',
                      width: 71.w,
                      height: 69.w,
                    ),
                    SizedBox(
                      width: 28.w,
                    ),
                    Text(
                      '添加照片',
                      style: TextStyle(
                          fontSize: 30.sp,
                          color: Color.fromRGBO(57, 57, 57, 1)),
                    )
                  ],
                ),
              ),
              onTap: () {
                selectImg();
              },
            ),
          ],
        ),
      ),
    );
  }

  //选择图片上传方式
  Future<void> selectImg() async {
    Get.bottomSheet<void>(Container(
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(10), topRight: Radius.circular(10))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: 100.w,
            padding: EdgeInsets.only(top: 47.w),
            child: Text(
              '选择操作',
              style: TextStyle(fontSize: 36.sp, fontWeight: FontWeight.w600),
            ),
          ),
          Row(
            children: [
              Container(
                padding: EdgeInsets.only(left: 186.w, top: 67.w, bottom: 53.w),
                child: InkWell(
                  onTap: () {
                    Get.back<void>();
                    getImage(source: ImageSource.camera, callback: () {});
                  },
                  child: Image.asset(
                    'assets/images/image_cutting/camera.png',
                    width: 94.w,
                    height: 85.w,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(left: 186.w, top: 67.w, bottom: 53.w),
                child: InkWell(
                  onTap: () {
                    Get.back<void>();
                    getImage(source: ImageSource.gallery, callback: () {});
                  },
                  child: Image.asset(
                    'assets/images/image_cutting/photo.png',
                    width: 94.w,
                    height: 85.w,
                  ),
                ),
              )
            ],
          ),
          Divider(
            height: 2.w,
          ),
          TextButton(
              onPressed: () {
                Get.back<void>();
              },
              child: Text(
                '取消',
                style: TextStyle(
                    fontSize: 32.w, color: const Color.fromRGBO(57, 57, 57, 1)),
              ))
        ],
      ),
    ));
  }

  ///选择图片
  Future getImage(
      {required ImageSource source, required Function callback}) async {
    final ImagePicker _picker = ImagePicker();
    final XFile? pickedFile = await _picker.pickImage(source: source);
    if (pickedFile != null) {
      ///压缩图片
      File file = await ImageCompressUtil.imageCompressAndGetFile(
          File(pickedFile.path));

      ///获取路径
      String path = file.path;

      ///获取名称
      String name = path.substring(path.lastIndexOf('/') + 1, path.length);

      ///获取后缀
      String suffix = name.substring(name.lastIndexOf('.') + 1, name.length);

      FormData formData = FormData.fromMap(<String, dynamic>{
        'name': name,
        'Filedata': await MultipartFile.fromFile(path,
            filename: name, contentType: MediaType.parse('image/$suffix')),
      });

      ///上传
      // await uploadPhoto(formData);

      callback();
    } else {}
  }

  Future _pickImage() async {
    final ImagePicker _picker = ImagePicker();
    final XFile? pickedFile =
        await _picker.pickImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      setState(() {});
    }
  }

  Future<Null> _cropImage(XFile? imageFile) async {
    File? croppedFile =
        await ImageCropper.cropImage(sourcePath: imageFile!.path);
  }
}

class ImageCuttingPageController extends GetxController {}
